package com.aglow.updateDownload.service;


import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.aglow.updateDownload.code.DownloadManager;
import com.aglow.updateDownload.listener.UpdateDownloadListener;

public class UpdateDownloadService extends Service {
    private String path;
    private String url;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        path = Environment.getExternalStorageDirectory() + "/chufeng/www.apk";
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        url = intent.getStringExtra("url");
        startDownLoad();
        return super.onStartCommand(intent, flags, startId);

    }


    /**
     * 下载
     */
    private void startDownLoad() {
        if (url == null) {
            return;
        }
        DownloadManager.getInstance().startDownload(url, path, new UpdateDownloadListener() {
            @Override
            public void onStarted() {
                Log.i("service", "下载开始");
            }

            @Override
            public void onProgressChanged(int progress, String downloadUrl) {
                Log.i("service", "进度条 = " + progress);
            }

            @Override
            public void onFinished(float completeSize, String downloadUrl) {
                Log.i("service", "下载完成 地址 = " + downloadUrl);
            }

            @Override
            public void onFailure() {
                Log.i("service", "下载失败");
            }
        });
    }

}
